package com.xiaoh.tcp;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author xiaoH
 * @date 2020/4/23
 * @description
 */
public class LineClientHandler extends ChannelInboundHandlerAdapter {

    private int count = 0;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // Send the message to Server
        for (int i = 0; i < 100; i++) {

            String msg = "hello from client " + i;
            System.out.println("client send message : " + msg);

            ctx.writeAndFlush(msg + System.getProperty("line.separator"));
        }

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        String body = (String) msg;
        count++;
        System.out.println("client read msg : " + body + ", count : " + count);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        System.out.println("client caught exception " + cause);
        ctx.close();
    }
}
